<template>
	<view style="background: #f6f6f8;width: 100%;height: 100vh;">
		<view v-if="orderList.length>0">
			<view class="tbc-flex-col" v-for="(item, index) in orderList" :key="index">
				<orderItem :item="item" :customInfo="item.customer_info" @refreshOrder="refreshOrder"></orderItem>
			</view>
		</view>
		<view class="blank_box" v-else>
			<view>No order</view>
		</view>
	</view>
</template>

<script>
  import orderItem from './transportOrderItem.vue';
	export default {
		components: {orderItem},
		data() {
			return {
				tabBarIndex: -1,
				tabBarCode: '',
				tabBarList: [{
						label: 'all',
						params: {
							order_status: 0,
							tab: "all"
						}
					},
					{
						label: 'waitPay',
						params: {
							order_status: 1,
							tab: '待付款'
						}
					},
					{
						label: 'waitSend',
						params: {
							order_status: 4,
							tab: '待发货'
						}
					},
					{
						label: 'waitSigned',
						params: {
							order_status: 6,
							tab: '待签收'
						}
					},
					{
						label: 'finished',
						params: {
							order_status: 7,
							tab: '已完成'
						}
					},
				],
				// 全部 已订货 待收货 已收货
				orderList: [],
				total: -1,
				page: 1,
				inRequest: false,
				refresh: 0,
				totalPrice:0,
			}
		},
		computed: {
			// ...mapGetters({
			// 	orderList: 'order/orderList'
			// })
			// totalPrice(){
			// 	let total = 0
			// 	this.orderList.forEach(item => {
			// 		total += Number(item.pay_money)
			// 	})
			// 	return utils.setPrice(total) 
			// }
		},
		onLoad: function(options) {
			let index = this.tabBarList.findIndex(item => item.params.tab === options.tab)
			if (index < 0) index = 0
		},
		onShow() {
			this.getOrderList()
		},
		onPullDownRefresh: function() {
      this.page=1
			this.initOrderList()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom: function() {
			this.getOrderList()
		},
		methods: {
      refreshOrder(){
        this.page=1
        this.initOrderList()
        setTimeout(function() {
          uni.stopPullDownRefresh();
        }, 1000);
      },
			async handleDelOrders(){
				let list=this.orderList.filter(v=>v.checked)
				let ids=list.map(v=>{
					return v.id
				})
				const res = await uni.$http.post('/order/del',{order_ids:ids})
				if (res.data.code !== 200) {
					return uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}else{
					this.getOrderList()
				}
			},
			selectOrderIds(orders){
				let item=this.orderList[orders.order_index]
				this.orderList[orders.order_index].checked=!item.checked
				let list=this.orderList.filter(v=>v.checked)
				let price_list=list.map(v=>{
					return v.pay_money
				})
				this.totalPrice=price_list.reduce((a,b)=>parseFloat(a)+parseFloat(b))
			},
			// 确认收货
			handleConfirm(data) {
				let order_id = data.order_id
				uni.showModal({
					title: "Tips",
					content: "Are you sure you have received the goods?",
					cancelText:'cancel',
					confirmText:'sure',
					success: async (res) => {
						if (res.confirm) {
							let resp = await uni.$http.post('/order/confirmOrder', {
								order_id
							})
							uni.showToast({
								icon: "none",
								title: resp.data.msg
							})
							if (resp.data.code == 200) {
								this.getOrderList()
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			showPaymentSelector(item) {
				let order = item.basic.data.field
				this.$refs.payment.show(order)
			},

			onTabBarClick(index, item) {
				if (this.tabBarIndex == index) return
				this.tabBarIndex = index
				this.tabBarCode = this.tabBarList[index].params.order_status
				this.$store.commit('order/setOrderList', null)
				this.initOrderList()
			},

			async initOrderList() {
				this.page = 1
				this.total = -1
				this.getOrderList()
			},
			async getOrderList() {
        this.$ajax(this.$http + '/order/pendingShipment?outbound_status=2', "GET", null, (response) => {
					let data = JSON.parse(response)
          this.orderList = data.data
				})
			},
			stopPullDownRefresh() {
				// #ifdef APP-NVUE
				this.$refs.list.stopPullDownRefresh()
				// #endif
				uni.stopPullDownRefresh()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.blank_box {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 400rpx;
		background: #fff;

		.blank_icon {
			width: 50%;
			margin-top: 200rpx;
			margin-bottom: 40rpx;
		}
	}
</style>